//
//  WTStatusToolbar.swift
//  WTSinaWeibo
//
//  Created by  Wellim on 16/6/29.
//  Copyright © 2016年  Wellim. All rights reserved.
//

import UIKit

class WTStatusToolbar: UIView {

    var retweetBtn: UIButton?
    var commentBtn: UIButton?
    var unlikeBtn: UIButton?

    // 供外界赋值
    var status: WTStatus?{
    
        didSet{
        
            retweetBtn?.setTitle(status?.repostsStr, forState: UIControlState.Normal)
            
            commentBtn?.setTitle(status?.commentsStr, forState: UIControlState.Normal)
            
            unlikeBtn?.setTitle(status?.repostsStr, forState: UIControlState.Normal)
        
        
        }
    
    }
    
  
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        getUpUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // MARK: - 设置视图
    func getUpUI()  {
        
         backgroundColor = UIColor.whiteColor()
        
        // 添加控件
   retweetBtn = addChileButtons("timeline_icon_retweet", title: "转发")
   commentBtn = addChileButtons("timeline_icon_comment", title: "评论")
   unlikeBtn = addChileButtons("timeline_icon_unlike", title: "赞")
        
         // 分割线
        let line1 = addChildLines()
        let line2 = addChildLines()
        
        // 添加约束
        retweetBtn?.snp_makeConstraints { (make) in
            make.top.left.bottom.equalTo(self)
            make.width.equalTo(commentBtn!)
        }
        
        commentBtn?.snp_makeConstraints { (make) in
           make.width.equalTo(unlikeBtn!)
           make.top.equalTo(self)
           make.left.equalTo(retweetBtn!.snp_right)
            
            
        }
        
        unlikeBtn?.snp_makeConstraints { (make) in
            
           make.top.right.bottom.equalTo(self)
           make.left.equalTo(commentBtn!.snp_right)
        }
        
        //分割线的约束
        line1.snp_makeConstraints { (make) in
        
            make.centerY.equalTo(self)
            make.centerX.equalTo(retweetBtn!.snp_right)
            
        }
        
        line2.snp_makeConstraints { (make) in
            
            make.centerY.equalTo(self)
            make.centerX.equalTo(commentBtn!.snp_right)
        }
        
        
    }
    
    // 创建button
    private func addChileButtons(imageName:String,title: String) ->UIButton{
     
        let button = UIButton()
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setTitle(title, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        // 设置背景图片
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: UIControlState.Normal)
      
        addSubview(button);
        
        return button
    
    }
    
    // 分割线
    
    private func addChildLines() -> UIImageView{
     
        let imageView = UIImageView(imageName: "timeline_card_bottom_line")
    
        addSubview(imageView)
        
        return imageView
    }
    
}
